package com.zhouk.httpClient;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

/**
 * Create By Zhouke on 2017/08/25
 */
public class JsoupParser {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public String getToken() {
        String formToken = null;
        try {
            Document doc = null;
            doc = Jsoup.connect("http://www.huacai.com.cn/index.php?m=vote&c=index&a=lists&siteid=1&keywords=%E7%94%A8%E5%8F%8B&from=groupmessage&isappinstalled=0").get();
            Element element = doc.getElementById("form_token");
            formToken = element.val();
            logger.info("token:{}", formToken);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return formToken;
    }

    public static boolean isOnline(String name){
        boolean isOnline = false;
        Document doc = Spider.getHtmlByUrl("https://www.douyu.com/directory/game/DOTA2");
        Elements elements = doc.select("span.dy-name");
        for (Element element : elements) {
            List<Node> nodes = element.childNodes();
            String value = nodes.get(0).attr("text");
            if(value.equalsIgnoreCase(name)) {
                return true;
            }
        }
        return isOnline;
    }


}
